<?php

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class SalaryExport implements FromCollection, WithHeadings, WithColumnWidths, WithStyles
{
    protected $data;

    public function __construct(Collection $data)
    {
        $this->data = $data;
    }

    public function collection()
    {
        return $this->data;
    }

    public function headings(): array
    {
        return [
            [
                '姓名', '英文名', '职位', '职级', '入职时间', '离职时间', '基本工资', '出勤天数', '加班天数', '',
                '公司', '', '', '', '', '', '', '', '',
                '应发工资',  '', '', '', '', '', '', '', '', '',
                '扣款项', '', '', '', '', '', '', '', '', '',
                '实发', '备注'
            ],
            [
                '', '', '', '', '', '', '', '', '休息日加班', '法定节假日加班',
                '公司养老', '公司医疗', '公司失业', '公司工伤', '公司生育', '公司公积金', '社保公积金补缴（公司）','社保公积金退费（公司）', '小计',
                '基本工资',  '岗位津贴', '加班工资', '餐补', '通讯补助', '全勤奖', '保密费', '奖金', '补贴', '小计',
                '养老保险', '失业保险', '医疗保险', '公积金', '考勤扣款', '其他', '个人所得税', '社保公积金补缴（个人）', '社保公积金退费（个人）', '小计',
                '', ''
            ]
        ];

    }

    public function styles(Worksheet $sheet)
    {
        $sheet->mergeCells('A1:A2');
        $sheet->mergeCells('B1:B2');
        $sheet->mergeCells('C1:C2');
        $sheet->mergeCells('D1:D2');
        $sheet->mergeCells('E1:E2');
        $sheet->mergeCells('F1:F2');
        $sheet->mergeCells('G1:G2');
        $sheet->mergeCells('H1:H2');
        $sheet->mergeCells('I1:J1');
        $sheet->mergeCells('K1:S1');
        $sheet->mergeCells('T1:AC1');
        $sheet->mergeCells('AD1:AM1');
        $sheet->mergeCells('AN1:AN2');
        $sheet->mergeCells('AO1:AO2');

        $sheet->getStyle('A1:AO2')->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:AO2')->applyFromArray(['alignment' => ['horizontal' => 'center']]);//设置水平居中
        $sheet->getStyle('A1:AO2')->applyFromArray(['font' => ['bold' => true]]);//字体设置
        $sheet->getCell('B2');//设置换行
        $sheet->getStyle('A2:AO2')->getAlignment()->setWrapText(true);

    }

    public function columnWidths(): array
    {
        // 设置每列的宽度
        return [
            'A' => 20,
            'B' => 20,
            'C' => 20,
            'D' => 20,
            'E' => 20,
            'F' => 20,
            'G' => 20,
            'H' => 20,
            'I' => 20,
            'J' => 20,
            'K' => 20,
            'L' => 20,
            'M' => 20,
            'N' => 20,
            'O' => 20,
            'P' => 20,
            'Q' => 20,
            'R' => 20,
            'S' => 20,
            'T' => 20,
            'U' => 20,
            'V' => 20,
            'W' => 20,
            'X' => 20,
            'Y' => 20,
            'Z' => 20,
            'AA' => 20,
            'AB' => 20,
            'AC' => 20,
            'AD' => 20,
            'AE' => 20,
            'AF' => 20,
            'AG' => 20,
            'AH' => 20,
            'AI' => 20,
            'AJ' => 20,
            'AK' => 20,
            'AL' => 20,
            'AM' => 20,
            'AN' => 20,
            'AO' => 20,
            // 根据你的需求设置其他列的宽度
        ];
    }

//    public function registerEvents(): array
//    {
//        return [
//            AfterSheet::class => function(AfterSheet $event) {
//                $event->sheet->getDelegate()->freezePane('A2');
//            },
//        ];
//    }

}